{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Project4 - Host program"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import time\n",
    "\n",
    "from pynq import Overlay\n",
    "import pynq.lib.dma\n",
    "from pynq import Xlnk\n",
    "from pynq import MMIO\n",
    "\n",
    "o1 = Overlay('/home/xilinx/jupyter_notebooks/OFDM/OFDM.bit')\n",
    "# Download your bitstream to FPGA\n",
    "t_before_bitstream = time.time()\n",
    "o1.download()\n",
    "t_after_bitstream = time.time()\n",
    "\n",
    "print(t_after_bitstream - t_before_bitstream, 'seconds to program bitstream')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xlnk = Xlnk() # Contiguous Memory Allocator (CMA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "length = 35000\n",
    "\n",
    "# Open input/output files\n",
    "fI = open('input.dat','r')\n",
    "\n",
    "fG = open('out.gold.dat', 'r')\n",
    "\n",
    "# Allocate regular numpy arrays to store input and output\n",
    "inp = np.empty([length,], dtype=np.float32)\n",
    "\n",
    "golden = np.empty([length,], dtype=np.float32)\n",
    "\n",
    "# Store data into arrays\n",
    "for i in range (0, length):\n",
    "    golden[i] = [np.float32(x) for x in next(fG).split()]\n",
    "    inp[i] = np.float32(next(fI))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(golden)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Complete the following block"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Allocate CMA array for DMA\n",
    "xlnk = Xlnk()\n",
    "# Copy regular numpy arrays to CMA arrays\n",
    "dma = o1.axi_dma_0\n",
    "# Write length using MMIO (we got the address from Vivado)\n",
    "OFDM_ip = MMIO(0x43c00000,0x10000)\n",
    "\n",
    "size = 35000\n",
    "in_buffer = xlnk.cma_array(shape=(size,), dtype=np.float32)\n",
    "out_buffer = xlnk.cma_array(shape=(size,), dtype=np.float32)\n",
    "\n",
    "fI = open('input.dat','r')\n",
    "fG = open('out.gold.dat', 'r')\n",
    "for i in range (0, size):\n",
    "    in_buffer[i] = np.float32(next(fI))\n",
    "    \n",
    "OFDM_ip.write(0x10,size)\n",
    "t_start = time.time()\n",
    "# Begin data transfer from/to DMA\n",
    "dma.sendchannel.transfer(in_buffer)\n",
    "\n",
    "dma.recvchannel.transfer(out_buffer)\n",
    "\n",
    "dma.sendchannel.wait()\n",
    "\n",
    "dma.recvchannel.wait()\n",
    "\n",
    "t_stop = time.time()\n",
    "\n",
    "# Free the CMA buffers\n",
    "in_buffer.close()\n",
    "\n",
    "out_buffer.close()\n",
    "\n",
    "\n",
    "print(t_after_bitstream - t_before_bitstream, 'seconds to execute on hardware')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(out_buffer)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}